package com.tianhai.db.index;

import com.tianhai.db.DiskManager;

public class InspectDisk {
    public static void main(String[] args) {
        try {
            DiskManager dm = new DiskManager("bplustree_test.db");
            
            // 读取页ID=1的原始数据
            byte[] data = dm.rawReadPage(1);
            
            System.out.println("页ID=1的前100字节:");
            for (int i = 0; i < 100 && i < data.length; i++) {
                System.out.print(String.format("%02X ", data[i]));
                if ((i + 1) % 20 == 0) System.out.println();
            }
            System.out.println("\n");
            
            // 手动解析
            java.nio.ByteBuffer buffer = java.nio.ByteBuffer.wrap(data);
            int nodeType = buffer.getInt();
            int isRoot = buffer.getInt();
            int keyCount = buffer.getInt();
            int nextLeaf = buffer.getInt();
            
            System.out.println("解析结果:");
            System.out.println("  nodeType: " + nodeType + " (" + (nodeType == 0 ? "LEAF" : "INTERNAL") + ")");
            System.out.println("  isRoot: " + isRoot);
            System.out.println("  keyCount: " + keyCount);
            System.out.println("  nextLeaf: " + nextLeaf);
            
            // 读取keys
            System.out.println("\n前10个键:");
            for (int i = 0; i < 10 && i < keyCount; i++) {
                System.out.print(buffer.getInt() + " ");
            }
            System.out.println();
            
            // 重新定位到valueCount的位置
            buffer.position(16 + keyCount * 4);
            int valueCount = buffer.getInt();
            System.out.println("\nvalueCount: " + valueCount);
            
            System.out.println("\n前10个值:");
            for (int i = 0; i < 10 && i < valueCount; i++) {
                System.out.print(buffer.getInt() + " ");
            }
            System.out.println();
            
            dm.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
